//
//  SelectionController.m
//  LoveTravel
//
//  Created by MS on 16-1-20.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "SelectionController.h"
#import "LGProductDetail_Controller.h"
#import "LGCollectionList_Controller.h"
#import "SelectionDataTool.h"
#import "LGTableView.h"
@interface SelectionController ()<LGTableViewDelegate>
@property (nonatomic,strong) NSMutableArray * itemsArray;
@property (nonatomic,weak) LGTableView * lgtableView;
@property (nonatomic,assign) NSInteger pages;
@end

@implementation SelectionController

-(LGTableView *)tableView
{
    if (_lgtableView==nil) {
        LGTableView * view = [[LGTableView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:view];
        _lgtableView = view;
        _lgtableView.delegate = self;
    }
    return _lgtableView;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
   
    self.navigationController.navigationBar.hidden = YES;
    self.itemsArray = [NSMutableArray array];
    
    [self loadSeletionData];
    
    [self addHeaderRefresh];
    [self addFootRefresh];
}

-(void)loadSeletionData
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [SelectionDataTool getSeletionDataWithPages:++_pages Succuss:^(NSArray *data) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [_lgtableView.tableView.mj_footer endRefreshing];
        [_lgtableView.tableView.mj_header endRefreshing];
        
        if (data.count) {
            [self.itemsArray addObjectsFromArray:data];
        }
        
        self.lgtableView.itemsArray = _itemsArray;
       
    } failure:^(NSError *error) {
        NSLog(@"loadSeletionData--%@",error);
    }];
}

-(void)addFootRefresh
{
    __weak typeof(self) weakSelf = self;
    self.tableView.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadSeletionData];
        
    }];
}

-(void)addHeaderRefresh
{
    __weak typeof(self) weakSelf = self;
    
    self.tableView.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pages = 0;
        [weakSelf.itemsArray removeAllObjects];
        [weakSelf loadSeletionData];
    }];
}


-(void)lgTableViewDidSelectItem_Recomm:(LGTableView *)lGTableView recomm:(RecommsData *)recomm index:(NSInteger)index
{
    LGProductDetail_Controller * vc = [[LGProductDetail_Controller alloc] init];
    vc.recomm_Data = recomm;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)lgTableViewDidSelectItem_Collection:(LGTableView *)lGTableView recomm_collection:(Collections_Model *)recomm_collection itemIndex:(NSInteger)index
{
    LGCollectionList_Controller * vc = [[LGCollectionList_Controller alloc] init];
    vc.collection_Data = recomm_collection;
    vc.hidesBottomBarWhenPushed = YES;
   
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
